import { DataTypes, Model } from "sequelize";
import sequelize from "../config";

// 定义用户模型
class User extends Model {
  declare id: number;
  declare username: string;
  declare password: string;
  declare name: string;
  declare authority: number;
  declare email: string;
  declare stars: number;
  declare commits_topic: number;
  declare commits_post: number;
  declare createdAt: Date;
  declare updatedAt: Date;
}

User.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    username: {
      type: DataTypes.STRING(50),
      allowNull: false,
      validate: {
        notEmpty: true,
      },
    },
    password: {
      type: DataTypes.STRING(100),
      allowNull: false,
      validate: {
        notEmpty: true,
      },
    },
    name: {
      type: DataTypes.STRING(50),
      allowNull: false,
      validate: {
        notEmpty: true,
      },
    },
    // 权限
    authority: {
      type: DataTypes.INTEGER,
      allowNull: false,
      validate: {
        notEmpty: true,
      },
    },
    email: {
      type: DataTypes.STRING(100),
      allowNull: false,
      unique: true,
      validate: {
        isEmail: true,
      },
    },
    stars: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    commits_topic: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
    commits_post: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 0,
    },
  },
  {
    sequelize,
    tableName: "users", // 数据库表名
    timestamps: true, // 自动添加createdAt和updatedAt
  }
);

export default User;
